package io.github.burukeyou.dataframe.util;

import java.util.List;
import java.util.Map;

import io.github.burukeyou.dataframe.iframe.item.FI2;
import io.github.burukeyou.dataframe.iframe.item.FI3;
import io.github.burukeyou.dataframe.iframe.item.FI4;
import java9.util.stream.Collectors;
import java9.util.stream.Stream;
import java9.util.stream.StreamSupport;

public class FrameUtil {
    private FrameUtil() {
    }

    public static <K, V> List<FI2<K, V>> toListFI2(Map<K, V> resultMap) {
        Stream<Map.Entry<K, V>> stream = StreamSupport.stream(resultMap.entrySet());
        return stream.map(e -> new FI2<>(e.getKey(), e.getValue()))
                .collect(Collectors.toList());
    }

    public static <K, J, V> List<FI3<K, J, V>> toListFI3(Map<K, Map<J, V>> map) {
        return StreamSupport.stream(map.entrySet())
                .flatMap(et -> {
                    return StreamSupport.stream(et.getValue().entrySet())
                            .map(subEt -> new FI3<>(et.getKey(), subEt.getKey(), subEt.getValue()));
                })
                .collect(Collectors.toList());
    }

    public static <K, J, H, V> List<FI4<K, J, H, V>> toListFI4(Map<K, Map<J, Map<H, V>>> map) {
        return StreamSupport.stream(map.entrySet())
                .flatMap(et -> {
                    return StreamSupport.stream(et.getValue().entrySet())
                            .flatMap(subEt -> {
                                return StreamSupport.stream(subEt.getValue().entrySet())
                                        .map(sub2Et -> new FI4<>(et.getKey(), subEt.getKey(), sub2Et.getKey(), sub2Et.getValue()));
                            });
                })
                .collect(Collectors.toList());
    }
}